
------------------------------------------------------
--  Author : Team B
--  Description :single play module 
------------------------------------------------------
display.setStatusBar(display.HiddenStatusBar)

module(...,package.seeall)

-- define local variable 

local mapGame = {}
local score
local scorePlayer2
local viewItem1,viewItem2,viewItem3,viewNumberItem1,viewNumberItem2,viewNumberItem3,
viewNumberItem1Player2,viewNumberItem2Player2,viewNumberItem3Player2
local numberItem1,numberItem2,numberItem3 
local numberItem1Player2,numberItem2Player2,numberItem3Player2
local scoreText
local scoreTextPlayer2
local widthItem = display.viewableContentWidth/20
local heightItem = display.viewableContentWidth/20
local minDisObstacle = 160
local maxDisObstacle = 180
local numberItem = 10
local scoreTextWidth = 40
local scoreTextHeight = 20
local backBtn,shareFaBtn,replayBtn
local score
local scorePlayer2
local countScore
local forceNinja = 10
	--use on accelerometer event
local const = 5
	--direct of force
local direct="left"
	--limit of force
local leftslop1 = 40
local leftslop2 = 80
local leftslop3 = 120
local rightslop1 = -40
local rightslop2 = -80
local rightslop3 = -120
local forceapply = 0
local die = false
local ninja
--force per frame
local changeState=0
local fpfr = 1

--print(widthItem)
-----------------------------------------------------------
-- Create Animation
-----------------------------------------------------------
 

function createMap()
	
	local score = 0
	local widthMap,heightMap =  math.random(40,display.viewableContentWidth - 40), 
													display.viewableContentHeight + 40;													
	 for i=1,numberItem,1 do 
			local j = math.random(1,3)
			if j == 1 then
				mapGame[i] = display.newImage("iteam1.png")
				mapGame[i].tag = 1
			elseif  j == 2 then
				mapGame[i] = display.newImage("item2.png")
				mapGame[i].tag = 2
			else 
				mapGame[i] = display.newImage("item3.png")
				mapGame[i].tag = 3
			end
			local yDis = math.random(minDisObstacle, maxDisObstacle )
			--yDis = 0
			mapGame[i].width = 30
		    mapGame[i].height = 30
			mapGame[i]:setReferencePoint( display.BottomLeftReferencePoint )
		    mapGame[i].x = math.random( 40, display.viewableContentWidth - 40)
		    mapGame[i].y = heightMap + yDis
		    heightMap =  heightMap + yDis
	end
	
end

       
--run animation
local function runAnimation()
	--ninja.rotation=180	
	ninja:prepare("RunSprite")
	ninja:play("RunSprite")	
end


--slop to left
local function slop2left1Animation()
	ninja:prepare("sloping1")
	ninja:play("sloping1")  	
end	

local function slop2left2Animation()
	ninja:prepare("sloping2")
	ninja:play("sloping2")  	
end	

local function slop2left3Animation()
	ninja:prepare("sloping3")
	ninja:play("sloping3")  	
end	

-- fall to left on way
local function fall2leftAnimation()
	ninja:prepare("FallSprite")
	ninja:play("FallSprite")  	
end	

--stand up 
local function standupAnimation()
	ninja:prepare("StandUp")
 	ninja:play("StandUp")  	
end 	

--slop to right
local function slop2right1Animation()
	ninja:prepare("rightSloping")
	--ninja.x=display.viewableContentWidth/2-ninja.width/2+80
	ninja:play("rightSloping")  	
end	

local function slop2right2Animation()
	ninja:prepare("rightSloping2")
	--ninja.x=display.viewableContentWidth/2-ninja.width/2+80
	ninja:play("rightSloping2")  	
end	

local function slop2right3Animation()
	ninja:prepare("rightSloping3")
	--ninja.x=display.viewableContentWidth/2-ninja.width/2+80
	ninja:play("rightSloping3")  	
end	

--fall to right on way
local function fall2rightAnimation()
		if ninja.xScale >0 then
			ninja.xScale=ninja.xScale*-1	
			ninja.x=display.viewableContentWidth/2-ninja.width/2+80
			ninja:prepare("FallSprite")
			ninja:play("FallSprite")  	
		end	
end	

-----------------------------------------------------------
-- check collision
-----------------------------------------------------------

local function checkCollisionWithObstack(x1,x2,y1,y2,x3,x4,y3,y4) 
	--print(x1..x2..y1..y2..x3..x4..y3..y4)
    if x1 <= x3 and x3 <= x2 and y1 <=y3 and y3<=y2 then
        return x3,y3
    elseif x1 <= x3 and x3 <= x2 and y1 <=y4 and y4<=y2 then
         return x3,y3
     elseif x1 <= x4 and x4 <= x2 and y1 <=y4 and y4<=y2 then
        return x3,y3
    elseif x1 <= x4 and x4 <= x2 and y1 <=y3 and y3<=y2 then
        return x3,y3
    elseif x3 <= x1 and x1 <= x4 and y3 <=y1 and y1<=y4 then 
        return x3,y3
    elseif x3 <= x1 and x1 <= x4 and y3 <=y2 and y2<=y4 then 
        return x3,y3
    elseif x3 <= x2 and x2 <= x4 and y3 <=y1 and y1<=y4 then
        return x3,y3
    elseif x3 <= x2 and x2 <= x4 and y3 <=y2 and y2<=y4 then
        return x3,y3    
    else 
        return -1,-1
    end
end

 
-- Text parameters
local labelx = 50
local x = 220
local y = 55
local fontSize = 24

local frameUpdate = false					-- used to update our Text Color (once per frame)

function onFrame()
		
		--print("shit1")
		countScore = countScore + 1
		if countScore == 30 then 
			score = score +1 
			countScore = 0
			--scoreText.text = "Score:"..score
		end
		scoreText.text = "Player 1's score:"..score
		--got change state of enemy
		if changeState==1 then
			viewNumberItem1Player2.text=numberItem1Player2
		elseif changeState==2 then
			viewNumberItem2Player2.text=numberItem2Player2
		elseif changeState==3 then
			viewNumberItem3Player2.text=numberItem3Player2
		elseif changeState==4 then
			scoreTextPlayer2.text="Player 2's score:"..scorePlayer2
			viewNumberItem1Player2.text=numberItem1Player2
			viewNumberItem2Player2.text=numberItem2Player2
			viewNumberItem3Player2.text=numberItem3Player2		
		end	
		changeState=0
		frameUpdate = true
		if die then print("hehe") 
		else
			if direct == "left" then 
				forceNinja = forceNinja + fpfr	
				forceapply = forceapply + fpfr	
			else 
				forceNinja = forceNinja - fpfr
			    forceapply = forceapply - fpfr
			end
		end
		ninja.x = ninja.x - forceapply
		
		for i = 1, numberItem, 1 do
			if mapGame[i] .y < display.viewableContentHeight/3 then
					mapGame[i].isVisible = false
					local j = math.random(1,3)
					if j == 1 then
						mapGame[i] = display.newImage("iteam1.png")
						mapGame[i].tag = 1
					elseif j == 2 then
						mapGame[i] = display.newImage("item2.png")
						mapGame[i].tag = 2
					else 
						mapGame[i] = display.newImage("item3.png")
						mapGame[i].tag = 3
					end
					mapGame[i].width = 30
	    			mapGame[i].height = 30
	    			mapGame[i]:setReferencePoint( display.BottomLeftReferencePoint )
					local yDis = math.random(minDisObstacle, maxDisObstacle )
					if i == 1 then 
	                    mapGame[i].y  = mapGame[numberItem].y + yDis
	                else 
	                    mapGame[i].y  = mapGame[i-1].y + yDis
	                end
	                mapGame[i].x = math.random(40,display.viewableContentWidth - 40)
	                mapGame[i].isVisible = true         
			else
				mapGame[i].y = mapGame[i].y - 0.5
				if mapGame[i].x < display.viewableContentWidth/2 then 
					 local view = ( display.viewableContentWidth/2 - mapGame[i].x )/(display.viewableContentHeight*2)
					 mapGame[i].x = mapGame[i].x + view
				elseif mapGame[i].x > display.viewableContentWidth/2 then
				   mapGame[i].x = mapGame[i].x - (mapGame[i].x - display.viewableContentWidth/2)/(display.viewableContentHeight*2)				 
				end
				if mapGame[i].isVisible then
				   local x,y = checkCollisionWithObstack(ninja.x,ninja.x+20,ninja.y-ninja.height/4 ,ninja.y,
											mapGame[i].x , mapGame[i].x + widthItem,mapGame[i].y -heightItem , mapGame[i].y);
				   if x ~= -1 and y ~= -1 then
				   	if mapGame[i].tag == 1 then
	                   	numberItem1 = numberItem1 + 1
	                       mapGame[i].isVisible = false
	                   	viewNumberItem1.text = numberItem1
	                   	return 1
	                   elseif mapGame[i].tag == 2 then
	                   	numberItem2 = numberItem2 + 1
	                   	mapGame[i].isVisible = false
	                   	viewNumberItem2.text = numberItem2
	                   	return 2
	                    elseif mapGame[i].tag == 3 then
	                       numberItem3 = numberItem3 + 1
	                       mapGame[i].isVisible = false
	                       viewNumberItem3.text = numberItem3
	                       return 3
	                    end       
				end
		end		
		end
		return 0
		end
end

local function onAccelerate( event )
		forceapply = const*event.yGravity
		if event.yGravity > 0 then 
			direct = "left"
		else 	
			direct = "right"
		end	
		forceNinja = forceNinja+forceapply
		if forceNinja >0 and forceNinja <=leftslop1 then 
			if ninja:sequence() ~= "sloping1" then
				
				slop2left1Animation()
			end	
		elseif forceNinja <0 and forceNinja >=rightslop1 then 
			if ninja:sequence()~="rightSloping" then
				slop2right1Animation()
			end	
		elseif forceNinja ==0 then 
			if ninja:sequence()~="RunSprite" then
				runAnimation()
			end	
		elseif forceNinja>leftslop1 and forceNinja<=leftslop2 then 
			if ninja:sequence()~="sloping2" then
				slop2left2Animation()
			end	
		elseif forceNinja<rightslop1 and forceNinja>=rightslop2 then 
			if ninja:sequence()~="rightSloping2" then
				slop2right2Animation()
			end	
		elseif forceNinja>leftslop2 and forceNinja<=leftslop3 then 
			if ninja:sequence()~="sloping3" then	
				slop2left3Animation()
			end	
		elseif forceNinja <rightslop2 and forceNinja >=rightslop3 then 
			if ninja:sequence()~="rightSloping3" then
				--viewNumberItem1.text = "Phai:"..forceNinja.."|"..rightslop2.."|"..rightslop3
				slop2right3Animation()
			end	
		elseif forceNinja>leftslop3 then 
			if ninja:sequence()~="FallSprite" then
				finishGame()
				fall2leftAnimation()
			end
			die=true
		elseif forceNinja<rightslop3 then 
			if ninja:sequence()~="FallSprite" then
				finishGame()
				fall2rightAnimation()
			end;
			die=true 			
		end							
		 frameUpdate = false		-- update done 	
	end

-----------------
--create UI button
-----------------
local function createUI( group)
	print("Hehe")
	local loqsprite = require('loq_sprite') -- load the sprite sheet module
    local widget = require ( "widget" )

    local nfactory = loqsprite.newFactory("fall","run","standup","sloping1","sloping2","sloping3",
													"rightSloping1","rightSloping2","rightSloping3")
    local baseline = 3*display.viewableContentHeight/4


--create Chi Pheo
		
	   createMap()
	   
	   print("Created map")
	   
		local backgroundGame = display.newImage( "background_ALL.png" )
        backgroundGame.width = display.viewableContentWidth
        backgroundGame.height = display.viewableContentHeight
	    backgroundGame:setReferencePoint( display.BottomLeftReferencePoint  )
	    --background.x, background.y = 0, display.viewableContentHeight
	    backgroundGame.x = 0
	    backgroundGame.y =  320
		
	   local backgroundLeft = display.newImage("background_LEFT.png")
	   backgroundLeft.width = display.viewableContentWidth
       backgroundLeft.height = display.viewableContentHeight
	   backgroundLeft:setReferencePoint( display.BottomLeftReferencePoint  )
	   --background.x, background.y = 0, display.viewableContentHeight
	   backgroundLeft.x = 0
	   backgroundLeft.y =  display.viewableContentHeight
	   
	   local backgroundRight = display.newImage("background_RIGHT.png")
	   backgroundRight.width = display.viewableContentWidth
       backgroundRight.height = display.viewableContentHeight
	   backgroundRight:setReferencePoint( display.BottomLeftReferencePoint  )
	   --background.x, background.y = 0, display.viewableContentHeight
	   backgroundRight.x = 0
	   backgroundRight.y =  display.viewableContentHeight
	   	   
	   print("Device:"..display.viewableContentWidth.."|"..display.viewableContentHeight)

		
		replayBtn= widget.newButton{
			label="replay",
			labelColor = { default={0}, over={128} },
			width = 100, height=30,
	        onPress = replayBtnPress
		}
		replayBtn:setReferencePoint( display.TopLeftReferencePoint )
		replayBtn.x = display.viewableContentWidth/3
		replayBtn.y = display.viewableContentHeight/3
		
		shareFaBtn= widget.newButton{
			label="shareFace",
			labelColor = { default={0}, over={128} },
			width=100, height = 30,
	        onPress = shareFaBtnPress
		}
		shareFaBtn:setReferencePoint( display.TopLeftReferencePoint )
		shareFaBtn.x = display.viewableContentWidth*2/3
		shareFaBtn.y = display.viewableContentHeight/3
		
		backBtn= widget.newButton{
			label="back",
			labelColor = { default={0}, over={128} },
			width=100, height=30,
	        onPress = backBtnPress
		}
		backBtn:setReferencePoint( display.TopLeftReferencePoint )
		backBtn.x = display.viewableContentWidth/2
		backBtn.y = display.viewableContentHeight/2
		
		viewItem1 = display.newImageRect("iteam1.png", widthItem , heightItem)
		viewItem1:setReferencePoint( display.TopLeftReferencePoint )
		viewItem1.x = display.viewableContentWidth/20
        viewItem1.y = display.viewableContentHeight/20
        
        viewNumberItem1 = display.newText("", viewItem1.x + widthItem + 20, viewItem1.y, native.systemFont, 16)
		viewNumberItem1:setReferencePoint( display.TopLeftReferencePoint )
		viewNumberItem1:setTextColor(255, 255, 255)
		viewNumberItem1.size = 16
		viewNumberItem1.x =  viewItem1.x + widthItem + 10
		viewNumberItem1.y = viewItem1.y 
        
        
        viewNumberItem1Player2 = display.newText("", viewNumberItem1.x + 30, viewNumberItem1.y, native.systemFont, 16)
		viewNumberItem1Player2:setReferencePoint( display.TopLeftReferencePoint )
		viewNumberItem1Player2:setTextColor(255, 255, 255)
		viewNumberItem1Player2.size = 16
		viewNumberItem1Player2.x =  viewNumberItem1.x + 20
		viewNumberItem1Player2.y = viewNumberItem1.y 
		
        viewItem2 = display.newImageRect("item2.png", widthItem , heightItem)
		viewItem2:setReferencePoint( display.TopLeftReferencePoint )
		viewItem2.x = display.viewableContentWidth/20
        viewItem2.y = viewItem1.y  + display.viewableContentHeight/10
        
        viewNumberItem2 = display.newText("", viewItem2.x + widthItem + 20, viewItem2.y, native.systemFont, 16)
		viewNumberItem2:setReferencePoint( display.TopLeftReferencePoint )
		viewNumberItem2:setTextColor(255, 255, 255)
		viewNumberItem2.size = 16
		viewNumberItem2.x =  viewItem2.x + widthItem + 10
		viewNumberItem2.y = viewItem2.y 
		
		viewNumberItem2Player2 = display.newText("", viewNumberItem2.x +30, viewNumberItem2.y,native.systemFont, 16)
		viewNumberItem2Player2:setReferencePoint( display.TopLeftReferencePoint )
		viewNumberItem2Player2:setTextColor(255, 255, 255)
		viewNumberItem2Player2.size = 16
		viewNumberItem2Player2.x =  viewNumberItem2.x + 20
		viewNumberItem2Player2.y = viewNumberItem2.y 
		
        viewItem3 = display.newImageRect("item3.png", widthItem , heightItem)
		viewItem3:setReferencePoint( display.TopLeftReferencePoint )
		viewItem3.x = display.viewableContentWidth/20
        viewItem3.y = viewItem2.y  + display.viewableContentHeight/10			
		
		viewNumberItem3 = display.newText("", viewItem3.x + widthItem + 20, viewItem3.y, native.systemFont, 16)
		viewNumberItem3:setReferencePoint( display.TopLeftReferencePoint )
		viewNumberItem3:setTextColor(255, 255, 255)
		viewNumberItem3.size = 16
		viewNumberItem3.x =  viewItem3.x + widthItem + 10
		viewNumberItem3.y = viewItem3.y
		
		viewNumberItem3Player2 = display.newText("", viewNumberItem3.x +30, viewNumberItem3.y, native.systemFont, 16)
		viewNumberItem3Player2:setReferencePoint( display.TopLeftReferencePoint )
		viewNumberItem3Player2:setTextColor(255, 255, 255)
		viewNumberItem3Player2.size = 16
		viewNumberItem3Player2.x =  viewNumberItem3.x + 20
		viewNumberItem3Player2.y = viewNumberItem3.y 
		--number of item of enemy
		
		
		
		
		
		scoreText = display.newText("", scoreTextWidth,  scoreTextHeight , native.systemFont, 16)
		scoreText:setReferencePoint( display.TopLeftReferencePoint )
		scoreText:setTextColor(255, 255, 255)
 	   scoreText.text = "Player1's Score:0"
		scoreText.size = 16
		scoreText.x =  viewNumberItem3Player2.x+10
		scoreText.y = viewNumberItem3.y +30
		
		scoreTextPlayer2=display.newText("", scoreTextWidth,  scoreTextHeight , native.systemFont, 16)
		scoreTextPlayer2:setReferencePoint( display.TopLeftReferencePoint )
		scoreTextPlayer2:setTextColor(255, 255, 255)
 	   scoreTextPlayer2.text = "Player2's Score:0"
		scoreTextPlayer2.size = 16
		scoreTextPlayer2.x =  scoreText.x
		scoreTextPlayer2.y = scoreText.y+20	
		
		ninja = nfactory:newSpriteGroup("RunSprite")
		--init force
		ninja.xScale = 0.25
		ninja.yScale = 0.25
		ninja:setReferencePoint( display.BottomLeftReferencePoint )
		ninja.x = display.viewableContentWidth/2
	    ninja.y = 2*display.viewableContentHeight/3
		runAnimation()
		replayBtn.isVisible = false
		backBtn.isVisible = false
		shareFaBtn.isVisible = false
		
		
end	

local function backBtnPress()
 	
 	return true
 end
 
 local function shareFaBtnPress()
 	-- Share facebook
	 return true
 end

local function  replayBtnPress()
		print("hehe")
		Runtime:removeEventListener ( "enterFrame", onFrame )
		Runtime:removeEventListener ("accelerometer", onAccelerate)
		for i=1,numberItem,1 do 
			mapGame[i]:removeSelf()
		end
		mainPlay()
	return true
end

function finishGame()
	backBtn.isVisible = true
	replayBtn.isVisible = true
	shareFaBtn.isVisible = true
	Runtime:removeEventListener ( "enterFrame", onFrame )
	Runtime:removeEventListener ("accelerometer", onAccelerate)
	return true
end


--------------------------
--main play
--------------------------

function mainPlay(group)
	print("singlePlayer")
	score,countScore = 0,0
	numberItem1 = 0
	numberItem2 = 0
	numberItem3 = 0
	numberItem1Player2=0
	numberItem2Player2=0
	numberItem3Player2=0
	---
	system.setAccelerometerInterval(45)
	forceNinja = 10
	--use on accelerometer event
	const = 5
	--direct of force
	direct="left"
	--limit of force
	leftslop1 = 40
	leftslop2 = 80
	leftslop3 = 120
	rightslop1 = -40
	rightslop2 = -80
	rightslop3 = -120
	forceapply = 0
	die = false
	--force per frame
	fpfr = 1
	numberItem = 10
	
	---
	print("Create")
	createUI(group)
	--runAnimation()
	viewNumberItem1.text = numberItem1
    viewNumberItem3.text = numberItem2
    viewNumberItem2.text = numberItem3
    viewNumberItem1Player2.text = numberItem1Player2
    viewNumberItem3Player2.text = numberItem2Player2
    viewNumberItem2Player2.text = numberItem3Player2
    
	print("Finish Create")
	--Runtime:addEventListener ( "enterFrame", onFrame )
	Runtime:addEventListener ("accelerometer", onAccelerate)
	
end

---------------------------
--set and get score,num of item
---------------------------
function getScore()
	return score
end

function setScorePlayer2(score)
	scorePlayer2=score
end

function getNumofItem1()
	return numberItem1
end

function getNumofItem2()
	return numberItem2
end

function getNumofItem3()
	return numberItem3
end

function setNumofItem1Player2(numb)
	numberItem1Player2=numb
end	

function setNumofItem2Player2(numb)
	numberItem2Player2=numb
end	

function setNumofItem3Player2(numb)
	numberItem3Player2=numb
end	

function setChangeState(item)
	changeState=item
end	

function getChangeState()
	return changeState
end